/*
 * CountryCities.cpp
 *
 *  Created on: Jul 15, 2010
 *      Author: howan
 */

//std::map example

#include <map>
#include <vector>
#include <string>
#include <sstream>
#include <fstream>
#include <iostream>
#include <algorithm>
#include <iterator>

typedef std::vector<std::string> Type_VectorStr;
typedef Type_VectorStr * Type_VectorStrPtr;
typedef std::pair<std::string, Type_VectorStrPtr> Type_Str_VectorStr_Pair;
typedef std::map<std::string, Type_VectorStrPtr> Type_Str_VectorStrPtr_Map;

int main(int argc, char ** argv)
    {
    std::ifstream inf("./city.txt");
    //store cities of countries
    Type_Str_VectorStrPtr_Map countryCities;
    std::string line;
    std::cout << "hello\n";
    //build map
    if (inf.is_open())
        {
        std::cout<< "File is opened.\n";
        }
    else
        {
        std::cout<< "File is NOT open.\n";
        }
    while (getline(inf, line))
        {
        std::istringstream lineStream(line);
        std::string country;
        lineStream >> country;
        Type_VectorStrPtr pCities = new Type_VectorStr();
        countryCities.insert(Type_Str_VectorStr_Pair(country, pCities));

        while (lineStream)
            {
            std::string city;
            lineStream >> city;
            countryCities[country]->push_back(city);
            }
        }

    //print map
    Type_Str_VectorStrPtr_Map::iterator it;
    for (it = countryCities.begin(); it != countryCities.end(); ++it)
        {
        std::string country;
        country = it->first;
        Type_VectorStrPtr pCities = it->second;
        std::cout << country << ":\n";

        copy(pCities->begin(), pCities->end(), std::ostream_iterator<
                std::string>(std::cout, "\t"));
        std::cout << "\n";
        }

    return 0;
    }
